package services;

import helpers.UrlHelper;

import java.io.IOException;
import java.net.URL;
import java.util.List;

import models.Blog;
import models.News;

import com.google.gdata.client.GoogleService;
import com.google.gdata.client.Query;
import com.google.gdata.client.blogger.BloggerService;
import com.google.gdata.data.Category;
import com.google.gdata.data.Entry;
import com.google.gdata.data.Feed;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

public class Blogger {
	private String blogId = "";
	
	private String username = "";
	
	private String password = "";
	
	private String blogName = "";
	
	private GoogleService googleService;
	
	public Blogger(Blog blog) {
		blogId = blog.getBlogId();
		username = blog.getUsername();
		password = blog.getPassword();
		blogName = blog.getBlogName();
		googleService = new GoogleService("blogger", blogName);
		try {
//			bloggerService.setUserCredentials("hoanbn.y1s@gmail.com", "0983767270");
			googleService.setUserCredentials(username, password);
		} catch (AuthenticationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public Entry createPost(News news) 
		throws ServiceException, IOException {
		  // Create the entry to insert
		  Entry myEntry = new Entry();
		  myEntry.setTitle(new PlainTextConstruct(news.getTitle()));
		  String content = news.getDescription();
		  content += "<div style=\"text-align: center;\"><b>Source:</b> " + UrlHelper.getDomainName(news.getUrl()) + "</div>";
		  myEntry.setContent(new PlainTextConstruct(content));
		  myEntry.setDraft(!news.getPublished());
		  Category category = new Category();
		  List<String> labels = news.getLabels();
		  for (int i = 0; i < labels.size(); i++) {
			  category = new Category();
			  category.setScheme("http://www.blogger.com/atom/ns#");
			  category.setTerm(labels.get(i));
			  myEntry.getCategories().add(category);
		  }
		  // Ask the service to insert the new entry
		  URL postUrl = new URL("http://www.blogger.com/feeds/" + blogId + "/posts/default");
		  return googleService.insert(postUrl, myEntry);
	}
	
	public void printAllPosts()
	    throws ServiceException, IOException {
	  // Request the feed
	  URL feedUrl = new URL("http://www.blogger.com/feeds/" + blogId + "/posts/default");
	  Feed resultFeed = googleService.getFeed(feedUrl, Feed.class);
	
	  // Print the results
	  System.out.println(resultFeed.getTitle().getPlainText());
	  for (int i = 0; i < resultFeed.getEntries().size(); i++) {
	    Entry entry = resultFeed.getEntries().get(i);
	    System.out.println("\t" + entry.getTitle().getPlainText());
	  }
	  System.out.println();
	}
	
	public Entry updatePostTitle(Entry entryToUpdate, String newTitle)
		throws ServiceException, IOException {
		entryToUpdate.setTitle(new PlainTextConstruct(newTitle));
		URL editUrl = new URL(entryToUpdate.getEditLink().getHref());
		return googleService.update(editUrl, entryToUpdate);
	}
	
	public void deletePost(String editLinkHref)
		throws ServiceException, IOException {
		URL deleteUrl = new URL(editLinkHref);
		googleService.delete(deleteUrl);
	}
	
	public void getEntryByTitle(String title)
		    throws ServiceException, IOException {
		// Create query and submit a request
		URL feedUrl = new URL("http://www.blogger.com/feeds/" + blogId + "/posts/default");
		Query myQuery = new Query(feedUrl);
		//myQuery.setFullTextQuery(title);
		Feed resultFeed = googleService.query(myQuery, Feed.class);

		// Print the results
		System.out.println(resultFeed.getTitle().getPlainText() + title);
		for (int i = 0; i < resultFeed.getEntries().size(); i++) {
		    Entry entry = resultFeed.getEntries().get(i);
		    System.out.println("\t" + entry.getTitle().getPlainText());
		    System.out.println("\t" + entry.getUpdated().toStringRfc822());
		}
		System.out.println();
	}
}
